package com.ybc.chinese.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ybc.chinese.R;
import com.ybc.chinese.adapter.CollectionAdapter;
import com.ybc.chinese.utils.CacheUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <p>我的收藏<p/>
 * Created by Yubch on 17/3/9.
 */
public class CollectionActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lv_collection)
    ListView lvCollection;
    @Bind(R.id.iv_left)
    ImageView ivLeft;

    public static final String COLLECTION_INDEX = "collection_index";
    private Context mCtx;
    private CollectionAdapter mAdapter;
    private List<String> collections = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        mCtx = this;
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        String jsonCollection = CacheUtil.getCache(getApplicationContext(), CollectionActivity.COLLECTION_INDEX);
        if(!TextUtils.isEmpty(jsonCollection)) {
            collections = new Gson().fromJson(jsonCollection, new TypeToken<List<String>>(){}.getType());
        }
        mAdapter.setData(collections);
    }

    private void initView() {
        tvTitle.setText(R.string.my_collection);
        ivLeft.setImageResource(R.mipmap.ic_back);
        ivLeft.setVisibility(View.VISIBLE);
        mAdapter = new CollectionAdapter(mCtx);
        lvCollection.setAdapter(mAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.iv_left)
    public void left() {
        finish();
    }
}
